package ui;

import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import messageprocessors.Server;

public class ServerUI extends JFrame implements Observer {

	private static final long serialVersionUID = -6551749494939739027L;

	public Server server;

	private JPanel panel; 
	private JTextArea tarea;
	private JScrollPane scrollpane;
	private int messageCounter;
	
	public ServerUI() {
		server = new Server();
		server.addObserver(this);
		messageCounter = 0;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO connect to xmpp server and start gui here
		ServerUI sui = new ServerUI();
		sui.initGUI();
		try {
			sui.server.connect();
		} catch (Exception e) {
			sui.showMessage(e.getMessage());
			e.printStackTrace();
		}
	}

	public void initGUI() {

		setSize(300, 300);
		setLocation(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setTitle("Server Status");

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
		
		tarea = new JTextArea();
		tarea.setText("Message will be displayed here!");
		
		
		scrollpane = new JScrollPane(tarea);
		panel.add(scrollpane);
		
		
		getContentPane().add(panel);
        
        setVisible(true);
           	
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		messageCounter++;
		
		String newText = tarea.getText() + "\n\n" + messageCounter + ") \n Last Message Received: \n" + server.getLastMessageReceived();
		newText += "\n Last Message Sent: \n" + server.getLastMessageSent();
		
		tarea.setText(newText);
	}
	
	public void showMessage(String text) {
		JOptionPane.showMessageDialog(this, text);
	}

}
